/**
 * @param {number[]} nums
 * @return {number}
*/
var mostFrequentEven = function(nums) {
  const map = new Map();
  for(let i = 0; i < nums.length; i++) {
    if(nums[i] % 2 === 0) {
      if(map.get(nums[i])) {
        map.set(nums[i], map.get(nums[i]) + 1);
      } else {
        map.set(nums[i], 1);
      }
    }
  }
  if(map.size == 0) return -1;
  else {
    const temp = [...map];
    // console.log('temp', temp);
    if(temp.length == 1) return temp[0][0];
    for(let i = 1; i < temp.length; i++) {
      if(temp[i][1] !== temp[i - 1][1]) break;
      if(i == temp.length - 1) {
        temp.sort((a, b) =>  a[0] - b[0]);
        return temp[0][0];
      }
    }
    const arr = temp.sort((a, b) =>  {
      if(b[1] == a[1]) {
        return a[0] - b[0];
      } else {
        return b[1] - a[1]
      }
    });
    return arr[0][0];
  }
};
let nums = [0,1,2,2,4,4,1];
console.log(mostFrequentEven(nums));
nums = [4,4,4,9,2,4];
console.log(mostFrequentEven(nums));
nums = [29,47,21,41,13,37,25,7];
console.log(mostFrequentEven(nums));
nums = [8154,9139,8194,3346,5450,9190,133,8239,4606,8671,8412,6290];
console.log(mostFrequentEven(nums));
nums = [7439,9849,3923,3070,8541,7674,5002,9657,2881,5468,4777,3724,3532,1384,4242,5759,3030,1233,9428,8629,9,8152,3033,94,8589,5999,7863,8782,3467,4988,3573,9301,5880,8752,3571,6290,9031,8645,1245,1658,9441,3558,2127,6364,1793,1261,3914,3036,6550,1919,9546,88,3061,4811,6200,9667,8452,282,9704,7789,7754,4680,9703,5809,8538,288,436,6618,2509,7757,3726,175,1107,7328,6220,3769,4297,8401,8672,9627,8691,8944,6519,8066,5018,3630,9204,502,3342,6650,2423,5262,8520,7417,423,1889,4629,3901,5901,5251,1013,5173,382,938,7425,2187,7685,4113,8814,4983,3291,6083,2603,2682,9248,5622,2884,4392,5002,4553,5319,4243,2853,4027,9437,2576,7031,7632,6059,9138,6132,1798,810,9299,8844,2480,7894,8098,40,896,2377,641,3365,3316,5118,4643,8208,7876,2220,5465,1849,1892,5451,2468,5504,7974,8519,4924,1034,2616,4697,149,3963,1175,5927,2566,7642,440,4084,4195,2568,1235,7977,6368,1493,3551,7712,7191,9246,8764,444,9095,9858,4316,2104,7906,3117,9106,372,1776,3179,5913,1110,6709,3933,8005,447,6216,9194,5287,8621,5719,2538,6186,2667,2324,1536,390,6969,6830,9690,7289,756,4900,5680,8412,8718,8063,6245,8600,7144,7812,1786,9283,9037,6522,1528,7045,6354,6213,1904,3229,602,955,2394,1113,1939,1081,3065,950,2132,3175,4304,6769,6724,1147,2934,6774,7806,946,1122,6832,2999,4064,152,279,8469,5035,7193,2316,9483,8439,2615,9107,6665,958,4988,5853,2236,1782,1188,2398,9456,7687,9426,9767,6189,3069,614,9903,4342,9607,9088,4671,538,3810,6515,6608,1111,4861];
console.log(mostFrequentEven(nums));
